package com.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.pojo.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Lexin Huang
 */
@Repository
public interface UserMapper extends BaseMapper<User>{
    /**
     * 根据用户名更新密码
     * @param username 用户名
     * @param password 密码
     * @return 影响的行数
     */
    @Update("Update user set user_password = #{newPassword} where user_username = #{username}")
    int updatePassword(@Param("username") String username, @Param("newPassword") String password);

    /**
     * 根据用户名查询用户
     * @param username 用户名
     * @return 用户对象(如果存在) 否则为null
     */
    @Results(id = "userMap", value = {
            @Result(column = "id", property = "id", javaType = Integer.class, id = true),
            @Result(column = "user_portrait_path", property = "portraitPath", javaType = String.class),
            @Result(column = "user_username", property = "username", javaType = String.class),
            @Result(column = "user_password", property = "password", javaType = String.class),
            @Result(column = "user_email_address", property = "emailAddress", javaType = String.class),
            @Result(column = "user_introduction", property = "introduction", javaType = String.class)
    })
    @Select("Select * from user where user_username = #{username}")
    User getByUsername(@Param("username") String username);


    /**
     * 根据用户名和密码的组合查询用户
     * @param username 用户名
     * @param password 密码
     * @return 用户对象(如果组合正确的话) 否则为 null
     */
    @ResultMap(value = "userMap")
    @Select("Select * from user where user_username = #{username} and user_password = #{password}")
    User getLoginUser(@Param("username") String username, @Param("password")String password);


    @ResultMap(value = "userMap")
    @Select("Select * from user where user_username like %#{name}%")
    List<User> selectUsernameLike(@Param("name")String name);
}
